package com.jiyongjun.bms.base.module.menu.controller;

import com.github.pagehelper.PageInfo;
import com.jiyongjun.bms.base.module.menu.entity.MenuEntity;
import com.jiyongjun.bms.base.module.menu.entity.MenuQueryModel;
import com.jiyongjun.bms.base.module.menu.service.MenuService;
import com.jiyongjun.bms.tool.ParamValidator;
import com.jiyongjun.bms.tool.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <h2>系统菜单表控制层</h2>
 * <p>
 *  描述：
 * </p>
 * 
 * @author 冀大侠
 * @since 1.0 2019-06-01 22:21:11
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping
    public PageInfo<MenuEntity> getByCondition(MenuQueryModel queryModel) {
        List<MenuEntity> list = menuService.getByCondition(queryModel);
        return new PageInfo<>(list);
    }

    @RequestMapping("/{id}")
    public MenuEntity get(@PathVariable("id") Long id) {
        MenuEntity entity = menuService.getById(id);
        return entity;
    }

    @PostMapping
    public MenuEntity add(MenuEntity entity) {
        entity.setCreateTime(new Date());
        entity = menuService.insert(entity);
        return entity;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        menuService.delete(id);
        return Result.success(1);
    }

    @PutMapping
    public Result<Integer> update(MenuEntity entity) {
        // TODO aop拦截Biz异常
        ParamValidator.checkRequired(entity.getId(), "菜单id不能为空");
        Integer effectRows = menuService.update(entity);
        entity.setModifiedTime(new Date());
        return Result.success(effectRows);
    }

    @PatchMapping
    public Result<Integer> patchUpdate(MenuEntity entity) {
        // TODO aop拦截Biz异常
        ParamValidator.checkRequired(entity.getId(), "菜单id不能为空");
        Integer effectRows = menuService.patchUpdate(entity);
        entity.setModifiedTime(new Date());
        return Result.success(effectRows);
    }
}